home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: news.sprintlink.net!news1!news
- From: rclark@iquest.net (Robert B. Clark)
- Subject: Re: C beginner needs your help ASAP
- X-Nntp-Posting-Host: ind-004-236-169.iquest.net
- Message-ID: <3129dd8a.1042915@news.iquest.net>
- Sender: news@iquest.net (News Admin)
- Organization: IQuest Internet, Inc.
- X-Newsreader: Forte Agent .99d/16.182
- References: <4g862f$p0b@risky.ecs.umass.edu>
- Date: Tue, 20 Feb 1996 15:45:18 GMT
-
- [Posted and emailed]
-
- On Sun, 18 Feb 96 11:35:32 GMT, sebag@ecs.umass.edu wrote:
-
- >I am a new C programmmer who desperately needs help.
- >I have been digging in the manuals for a way to do this but have still
- >come out empty handed. Here is the problem: I want to open files in a while
- >loop with different filenames. data0,data1,data2,data3, .. data1000 , ...
- >I need to increment an integer each time around then convert it to a string
- >and then somehow use strcat to combine the "data" with the integer string.
-
- Are you trying to create unique temporary filenames? If so, look up the
- tmpnam() function. It will automatically increment filenames for you,
- making sure that the derived filename does not match any in the target
- directory.
-
- Otherwise, here's a quick sketch that uses sprintf() to create a
- formatted string named 'filename':
-
- #include <stdio.h>
- int main(void)
- {
- int ctr;
- char filename[13], basename[]="data";
-
- for (ctr=0; ctr<=1000; ctr++) /* Format & display 1001 filenames */
- {
- sprintf(filename,"%s%04d",basename,ctr);
- printf("\nThe derived filename is \"%s\"",filename);
- }
- return ctr>0;
- }
-
- This will produce strings "data0000", "data0001", etc. If you don't
- want the numeric suffix to be left-padded with zeroes, change the "%04d"
- specifier in the sprintf() statement to "%d".
- --
- Robert B. Clark <rclark@iquest.net>
- "Be wary of strong spirits. It can make you shoot at tax collectors...
- and miss." --RAH
-